package com.wan37.logic.chat.dao.impl;

import com.wan37.logic.chat.dao.WorldChatDao;
import com.wan37.logic.chat.domain.entity.WorldChatDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-27 11:00
 **/
@Transactional(propagation = Propagation.SUPPORTS, readOnly = false, rollbackFor = Exception.class)
@Repository
public class WorldChatDaoImpl implements WorldChatDao {
    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    public List<WorldChatDO> getWorldChatByUserId(String userId) {
        return (List<WorldChatDO>) hibernateTemplate.find("from WorldChatDO where userId = ?", userId);
    }

    @Override
    public boolean add(WorldChatDO worldChatDO) {
        try {
            hibernateTemplate.save(worldChatDO);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer id) {
        try {
            hibernateTemplate.save(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(WorldChatDO worldChatDO) {
        try {
            hibernateTemplate.save(worldChatDO);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<WorldChatDO> getAll() {
        return (List<WorldChatDO>) hibernateTemplate.find("from WorldChatDO");
    }
}
